/**
 * 
 */
package com.tocea.formation.DAO;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tocea.formation.model.Calendar;
import com.tocea.formation.model.Person;

/**
 * @author INSTA
 * 
 */
public class DAO {
	private static Logger LOGGER = LoggerFactory.getLogger(DAO.class);
	Session session = HibernateUtil.getSession();
	/**
	 * @param _personne
	 * @return
	 */
	public boolean enregistrerPerson(Person _personne) {
		boolean save = false;
		session.beginTransaction();
		try {
			session.saveOrUpdate(_personne);
			session.getTransaction().commit();
			save = true;
		} catch (Exception e) {
			save = false;
			e.printStackTrace();

		}

		session.close();
		return save;
	}
	/**
	 * @param _nom
	 * @param _prenom
	 * @param _mail
	 * @param _pswrd
	 * @return
	 * 
	 */
	public boolean personExiste(String _nom, String _prenom, String _mail,
			String _pswrd) {
		boolean existe = false;
		// Interrogation DB mode namedQuery
		Query namedQuery = session.getNamedQuery("getPerson");
		namedQuery.setString(0, _nom);
		namedQuery.setString(1, _prenom);
		namedQuery.setString(2, _mail);
		namedQuery.setString(3, _pswrd);

		@SuppressWarnings("unchecked")
		List<Person> listPerson = namedQuery.list();

		if (listPerson.size() > 0) {
			existe = true;
		}
		return existe;
	}
	/**
	 * @param _mail
	 * @param _pswrd
	 * @return
	 */
	public boolean personExiste(String _mail, String _pswrd) {
		boolean existe = false;
		// Interrogation DB mode namedQuery
		Query namedQuery = session.getNamedQuery("getPersonForConn");
		namedQuery.setString(0, _mail);
		namedQuery.setString(1, _pswrd);

		@SuppressWarnings("unchecked")
		List<Person> listPerson = namedQuery.list();

		// LOGGER.info("nb person existe : " + namedQuery.list().size());
		if (listPerson.size() > 0) {
			existe = true;
		}
		return existe;
	}
	/**
	 * @param _mail
	 * @param _pswrd
	 * @return
	 */
	public Person getPerson(String _mail, String _pswrd) {
		Person personne = null;
		// Interrogation DB mode namedQuery
		Query namedQuery = session.getNamedQuery("getPersonForConn");
		namedQuery.setString(0, _mail);
		namedQuery.setString(1, _pswrd);
		if (namedQuery.list().size() == 1) {
			personne = (Person) namedQuery.list().get(0);
		}
		return personne;
	}
	/**
	 * @param _calendar
	 * @return
	 */
	public boolean createCalendar(Calendar _calendar) {

		boolean save = false;
		session.beginTransaction();
		try {
			session.saveOrUpdate(_calendar);
			session.getTransaction().commit();
			save = true;
		} catch (Exception e) {
			save = false;
			e.printStackTrace();

		}

		session.close();
		return save;

	}
	public List<Calendar> getCalendars(Person _instance) {

		Query namedQuery = session.getNamedQuery("getCalendars");

		namedQuery.setString(0, Person.getInstance().getID().toString());
		System.out.println("La requete : " + namedQuery.getQueryString());
		List<Calendar> listCaledar = namedQuery.list();

		return listCaledar;

	}
}
